<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="主体编号" prop="makretNo">
        <el-input v-model="queryParams.makretNo" placeholder="请输入主体编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="主体名称" prop="marketName">
        <el-input v-model="queryParams.marketName" placeholder="请输入主体名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="商户手机号" prop="marketPhone">
        <el-input v-model="queryParams.marketPhone" placeholder="请输入商户手机号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="经营者" prop="makrketHeader">
        <el-input v-model="queryParams.makrketHeader" placeholder="请输入经营者" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="营业执照号" prop="marketBusinessLicenseNo">
        <el-input v-model="queryParams.marketBusinessLicenseNo" placeholder="请输入营业执照号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="七天内是否覆盖" prop="sevenDayCover">
        <el-select v-model="queryParams.sevenDayCover" placeholder="请选择七天内是否覆盖" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.SEVEN_DAY_COVER)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="营业状态" prop="makrketStatus">
        <el-select v-model="queryParams.makrketStatus" placeholder="请选择营业状态" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.BUSINESS_STATUS)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="商户地址" prop="marketAdress">
        <el-input v-model="queryParams.marketAdress" placeholder="请输入商户地址" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="所属地区" prop="areaId">
        <el-select v-model="queryParams.areaId" placeholder="请选择所属地区" clearable>
          <el-option v-for="dict in areaList" :key="dict.id" :label="dict.areaName" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="所属市场" prop="marketAffiliationId">
        <el-select v-model="queryParams.marketAffiliationId" placeholder="请选择所属市场" clearable>
          <el-option v-for="dict in categoryList" :key="dict.id" :label="dict.affiliationName" :value="dict.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="经营范围" prop="marketTag">
        <!-- <el-select v-model="queryParams.marketTag" placeholder="请选择经营范围" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.MARKET_SCOPE)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select> -->
        <el-input v-model="queryParams.marketTag" placeholder="请输入经营范围" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="市场分类" prop="marketType">
        <el-select v-model="queryParams.marketType" placeholder="请选择市场分类" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.MARKET_TYPE)" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="openForm(undefined)"
          v-hasPermi="['market:info:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          :loading="exportLoading" v-hasPermi="['market:info:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="主体编号" align="center" prop="makretNo" />
      <el-table-column label="主体名称" align="center" prop="marketName" />
      <!-- <el-table-column label="商户手机号" align="center" prop="marketPhone" /> -->
      <el-table-column label="经营者" align="center" prop="makrketHeader" />
      <!-- <el-table-column label="身份证正面" align="center" prop="idCardFront" /> -->
      <!-- <el-table-column label="身份证背面" align="center" prop="idCardBack" /> -->
      <!-- <el-table-column label="数据来源id" align="center" prop="dataSourceId">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.DATA_SOURCE_TYPE" :value="scope.row.dataSourceId" />
        </template>
</el-table-column> -->
      <!-- <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="营业执照" align="center" prop="marketBusinessLicenseImg" /> -->
      <el-table-column label="营业执照号" align="center" prop="marketBusinessLicenseNo" />
      <el-table-column label="七天内是否覆盖" align="center" prop="sevenDayCover">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.SEVEN_DAY_COVER" :value="scope.row.sevenDayCover" />
        </template>
      </el-table-column>
      <el-table-column label="营业状态" align="center" prop="makrketStatus">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.BUSINESS_STATUS" :value="scope.row.makrketStatus" />
        </template>
      </el-table-column>
      <el-table-column label="商户地址" align="center" prop="marketAdress" />
      <el-table-column label="所属地区" align="center" prop="marketAreaName" />
      <el-table-column label="所属市场" align="center" prop="marketAffiliationName" />
      <el-table-column label="经营范围" align="center" prop="marketTag">
      </el-table-column>
      <el-table-column label="市场分类" align="center" prop="marketType">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.MARKET_TYPE" :value="scope.row.marketType" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="openForm(scope.row.id)"
            v-hasPermi="['market:info:update']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['market:info:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 对话框(添加 / 修改) -->
    <InfoForm ref="formRef" @success="getList" />
  </div>
</template>

<script>
import * as InfoApi from '@/api/market/MarketInfo';
import * as AreaCategoryApi from '@/api/market/AreaCategory';
import * as AffiliationCategoryApi from '@/api/market/AffiliationCategory';
import TreeSelect from "@riophae/vue-treeselect";
import InfoForm from './InfoForm.vue';
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  name: "Info",
  components: {
    InfoForm,
    TreeSelect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 主体信息列表
      list: [],
      areaList: [],
      categoryList: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        makretNo: null,
        marketName: null,
        marketPhone: null,
        makrketHeader: null,
        idCardFront: null,
        idCardBack: null,
        dataSourceId: null,
        createTime: [],
        marketBusinessLicenseImg: null,
        marketBusinessLicenseNo: null,
        sevenDayCover: null,
        makrketStatus: null,
        marketAdress: null,
        marketAreaId: null,
        marketAffiliationId: null,
        marketTag: null,
        marketType: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    async getList() {
      try {
        this.loading = true;
        const list = await AreaCategoryApi.getAreaCategoryList();
        this.areaList = list.data
        const categoryList = await AffiliationCategoryApi.getAffiliationCategorylist();
        this.categoryList = categoryList.data
        const res = await InfoApi.getInfoPage(this.queryParams);
        this.list = res.data.list;
        this.total = res.data.total;
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      console.log(this.queryParams.marketAffiliationId, 'ID')
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认删除主体信息编号为"' + id + '"的数据项?')
      try {
        await InfoApi.deleteInfo(id);
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch { }
    },
    /** 导出按钮操作 */
    async handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有主体信息数据项?').then(() => {
        this.exportLoading = true;
        return InfoApi.exportInfoExcel(queryParams);
      }).then(response => {
        this.$download.excel(response, '主体信息.xls');
        this.exportLoading = false;
      }).catch(() => { });
    },
    // /** 获得市场区域分类树 */
    // async getAreaCategoryTree() {
    //   this.marketAffiliationTree = [];
    //   const res = await AreaCategoryApi.getAreaCategoryList();
    //   this.areaList.children = this.handleTree(res.data, 'id', 'parentId')
    //   console.log(this.areaList,'地区列表')
    //   const root = { id: 0, areaName: '顶级市场区域分类', children: [] };
    //   root.children = this.handleTree(res.data, 'id', 'parentId')
    //   this.marketAffiliationTree.push(root)
    //   console.log(this.marketAffiliationTree, 111)
    // },
    // /** 转换市场区域分类数据结构 */
    // normalizer(node) {
    //   if (node.children && !node.children.length) {
    //     delete node.children;
    //   }
    //   return {
    //     id: node.id,
    //     label: node['areaName'],
    //     children: node.children
    //   };
    // },
  }
};
</script>
